home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 026-050 / scopedisk33 / dutils / findit.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  4KB  |  162 lines

  1. /*
  2.  *  FINDIT()
  3.  *
  4.  *  FINDIT [-ddir] <programname> <programname> ...
  5.  *
  6.  *  Search specified directories (those specified in FINDITVOLS if none
  7.  *  spcified on the command line) for the specified program names.  Wildcards
  8.  *  are acceptable
  9.  *
  10.  *  Format for FINDITVOLS:    dir,dir,dir ...
  11.  */
  12.  
  13. #include <local/typedefs.h>
  14. #include <local/xmisc.h>
  15. #include <stdio.h>
  16.  
  17. #define ENVVAR    "FINDITVOLS"
  18.  
  19. MLIST    DList;
  20. MLIST    FList;
  21. char *ErrorString = "unable to open dres.library";
  22. char *EnvStr;
  23. char Path[1024];
  24.  
  25. extern int Enable_Abort;
  26.  
  27. main(ac,av)
  28. char *av[];
  29. {
  30.     NewList(&DList);
  31.     NewList(&FList);
  32.  
  33.     Enable_Abort = 0;
  34.     {
  35.     register short i;
  36.     for (i = 1; i < ac; ++i) {
  37.         register char *str = av[i];
  38.         if (*str != '-') {
  39.         register NODE *node = malloc(sizeof(NODE));
  40.         node->ln_Name = str;
  41.         AddTail(&FList, node);
  42.         continue;
  43.         }
  44.         for (++str; *str; ++str) {
  45.         switch(*str) {
  46.         case 'd':
  47.             {
  48.             register NODE *node = malloc(sizeof(NODE));
  49.             node->ln_Name = str + 1;
  50.             AddTail(&DList, node);
  51.             }
  52.             str = "\0";
  53.             break;
  54.         default:
  55.             printf("Unknown option: -%c\n", *str);
  56.             exit(1);
  57.         }
  58.         }
  59.     }
  60.     }
  61.     if (openlibs(DRES_LIB) == 0) {
  62.     puts(ErrorString);
  63.     exit(1);
  64.     }
  65.     mountrequest(0);
  66.     if (!GetHead(&DList)) {         /*  scan enviroment variable for dirs */
  67.     char *str;
  68.     register char *ptr;
  69.     register NODE *node;
  70.     register char c;
  71.  
  72.     str = EnvStr = GetDEnv(ENVVAR);
  73.     if (!str) {
  74.         printf("Env. Var %s not found, format:  dir,dir,dir...\n", ENVVAR);
  75.         goto fail;
  76.     }
  77.     for (c = 1; c; str = ptr + 1) {
  78.         for (ptr = str; *ptr && *ptr != ','; ++ptr);
  79.         c = *ptr;
  80.         *ptr = 0;
  81.         node = malloc(sizeof(NODE));
  82.         node->ln_Name = str;
  83.         AddTail(&DList, node);
  84.     }
  85.     }
  86.     {
  87.     register NODE *node;
  88.     register FIB *fib = malloc(sizeof(FIB));
  89.     while (node = RemHead(&DList)) {
  90.         long lock;
  91.  
  92.         if (lock = Lock(node->ln_Name, SHARED_LOCK)) {
  93.         strcpy(Path, node->ln_Name);
  94.         if (Examine(lock, fib))
  95.             SearchTree(lock, fib, strlen(Path));
  96.         UnLock(lock);
  97.         }
  98.         free(node);
  99.         if (checkbreak())
  100.         break;
  101.     }
  102.     free(fib);
  103.     puts("");
  104.     }
  105. fail:
  106.     if (checkbreak())
  107.     puts("^C");
  108.     {
  109.     register NODE *node;
  110.     while (node = RemHead(&DList))
  111.         free(node);
  112.     while (node = RemHead(&FList))
  113.         free(node);
  114.     }
  115.     if (EnvStr)
  116.     free(EnvStr);
  117.     mountrequest(1);
  118.     closelibs(-1);
  119. }
  120.  
  121. /*
  122.  *  Search the specified directory for the wildcarded names in FList.
  123.  */
  124.  
  125. SearchTree(dirlock, dirfib, idx)
  126. long dirlock;
  127. FIB *dirfib;
  128. {
  129.     long oldlock;
  130.     long lock;
  131.     register FIB *fib = malloc(sizeof(FIB));
  132.  
  133.     oldlock = CurrentDir(dirlock);
  134.     while (ExNext(dirlock, dirfib)) {
  135.     if (idx && Path[idx-1] != ':' && Path[idx-1] != '/') {
  136.         Path[idx] = '/';
  137.         strcpy(Path+idx+1, dirfib->fib_FileName);
  138.     } else {
  139.         strcpy(Path+idx, dirfib->fib_FileName);
  140.     }
  141.     if (dirfib->fib_DirEntryType > 0) {
  142.         if (lock = Lock(dirfib->fib_FileName, SHARED_LOCK)) {
  143.         if (Examine(lock, fib))
  144.             SearchTree(lock, fib, idx + strlen(Path+idx));
  145.         UnLock(lock);
  146.         if (checkbreak())
  147.             break;
  148.         }
  149.     } else {
  150.         register NODE *node;
  151.         for (node = GetHead(&FList); node; node = GetSucc(node)) {
  152.         if (WildCmp(node->ln_Name, dirfib->fib_FileName)) {
  153.             printf("%s ", Path);
  154.             fflush(stdout);
  155.         }
  156.         }
  157.     }
  158.     }
  159.     CurrentDir(oldlock);
  160.     free(fib);
  161. }
  162.